package info.fastpace.utils.entity;

import java.util.ArrayList;
import java.util.List;

public interface ListProvider<E extends Entity> extends Feature {

	List<E> getEntities();

	class Util {
		private Util() {
		}

		public static <E extends Entity> ListProvider<E> validateSupport(Entity entity) {
			ListProvider<E> fileAggregator = getFeature(entity);
			if (fileAggregator == null) {
				throw new RuntimeException("Using Files intent with an entity that does not support " + ListProvider.class.getName());
			}
			return fileAggregator;
		}

		public static <E extends Entity> ListProvider<E> getFeature(Entity entity) {
			if (entity == null) {
				return null;
			}
			@SuppressWarnings("unchecked")
			ListProvider<E> fileAggregator = (ListProvider<E>) entity.getFeature(ListProvider.class);
			return fileAggregator;
		}

		public static boolean equals(Class<? extends Feature> featureClass) {
			return featureClass.equals(ListProvider.class);
		}

		public static class ListProviderDefault<E extends Entity> implements ListProvider<E> {

			@Override
			public List<E> getEntities() {
				return new ArrayList<E>();
			}
		}

		public static class Decorator<E extends Entity, F extends ListProvider<E>> extends info.fastpace.utils.Decorator<F> implements ListProvider<E> {
			public Decorator(F feature) {
				super(feature);
			}

			@Override
			public List<E> getEntities() {
				return getInner().getEntities();
			}

		}

		public static class DecoratorSimple extends Decorator<Entity, ListProvider<Entity>> {
			public DecoratorSimple(ListProvider<Entity> feature) {
				super(feature);
			}
		}
	}
}
